package com.crm.biz.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.crm.model.BizInfosBelong;
import com.crm.model.BrandSchoolVo;
import com.crm.service.BizInfosBelongService;
import com.crm.util.JsonUtil;
import com.crm.util.ResponseUtil;
import com.crm.util.ValidateUtil;
import com.github.pagehelper.Page;

@Controller
@RequestMapping("/bizInfosBelong")
public class BizInfosBelongController {
	
	@Autowired
	private BizInfosBelongService bizInfosBelongService;
	
	@RequestMapping("/index")
	public String index(){
		
		return "configuration/infoQuantity";
	}
	
	@RequestMapping("/getAll")
	@ResponseBody
	public void getAll(HttpServletResponse response,HttpServletRequest request, Page<BizInfosBelong> page, BizInfosBelong t){
		Page<BizInfosBelong> list = (Page<BizInfosBelong>) bizInfosBelongService.selectAll(page, t);
		
		ResponseUtil.resultPage(response,request,list);
	}
	
	@RequestMapping("/addNewRecord")
	@ResponseBody
	public Map<String, Object> addNewRecord(BizInfosBelong record, BrandSchoolVo brandSchool){
		
		Map<String, Object> map = new HashMap<>();
		
		if(ValidateUtil.isEmpty(record.getInfosBelongName()))
			throw new RuntimeException("名称不能为空！");
		
		if(ValidateUtil.isEmpty(record.getDepartmentId()))
			throw new RuntimeException("部门不能为空！");
		
		if(ValidateUtil.isEmpty(record.getProjects()))
			throw new RuntimeException("项目不能为空！");
		
		record.setBrandSchool(JsonUtil.toJsonStr(brandSchool.getBrandSchools()));
		
		if(!ValidateUtil.isEmpty(record.getInfosBelongId())){
			bizInfosBelongService.updateByPrimaryKey(record);
		}else{
			bizInfosBelongService.insert(record);
		}
		
		map.put("status", "success");
		map.put("msg", "新增成功！");
		
		return map;
	}
	
	@RequestMapping("/deleteRecord")
	@ResponseBody
	public Map<String, Object> deleteRecord(BizInfosBelong record){
		
		Map<String, Object> map = new HashMap<>();
		
		record.setDeleteMark(0);
		bizInfosBelongService.updateByPrimaryKey(record);
		
		map.put("status", "success");
		map.put("msg", "删除成功！");
		
		return map;
	}
	
	@RequestMapping("/updateRecord")
	@ResponseBody
	public Map<String, Object> updateRecord(BizInfosBelong record){
		
		Map<String, Object> map = new HashMap<>();
		
		bizInfosBelongService.updateByPrimaryKey(record);
		
		map.put("status", "success");
		map.put("msg", "修改成功！");
		
		return map;
	}
	
	@RequestMapping("/getAllOption")
	@ResponseBody
	public Map<String, Object> getAllOption(Page<BizInfosBelong> page, BizInfosBelong record){
		Map<String, Object> map = new HashMap<>();
		Page<BizInfosBelong> list = (Page<BizInfosBelong>)bizInfosBelongService.selectAll(page, record);
		map.put("list", list);
		map.put("status", "success");
		map.put("msg", "获取成功！");	
		return map;
	}
}
